<?php
# http://projects:8081/cdapp-2kt-api/api/activity_history_operate.php
include_once './_portal.php';

$method = isset($_GET['method']) && $_GET['method'] == 'save' ?
'save' :
'get';

if ($method == 'get') {
  $start = $post->start ?? (date('Y-m-d') . ' 00:00:00');
  $end   = $post->end ?? (date('Y-m-d') . ' 23:59:59');
  handle_get($start, $end);
} elseif ($method == 'save') {
  $number      = $post->number;
  $startTime   = $post->startTime;
  $endTime     = $post->endTime;
  $flightCount = $post->flightCount;

  $sql = "UPDATE `2kt_activity_history` SET `startTime`='{$startTime}', `endTime`='{$endTime}', `flightCount`='{$flightCount}' WHERE `number`={$number}";

  $result = exec_sql($sql);
  output(['result' => $result, 'sql' => $sql]);

}

function handle_get($start, $end) {
  $sql    = "SELECT * FROM `2kt_activity_history` WHERE `startTime` BETWEEN '{$start}' AND '{$end}'";
  $result = exec_sql($sql) ?? [];
  output(['content' => $result, 'sql' => $sql]);
}

// "CREATE TABLE IF NOT EXISTS `cdapp`.`2kt_activity_history` (`number` INT(8) NOT NULL AUTO_INCREMENT , `unit` TEXT NOT NULL , `callsign` TEXT NOT NULL , `category` VARCHAR(4) NOT NULL , `startTime` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , `endTime` DATETIME NULL DEFAULT NULL , `flightCount` INT(5) NOT NULL DEFAULT '0' , PRIMARY KEY (`number`)) ENGINE = InnoDB"